//
//  configViewController.m
//  RealEstateQa
//
//  Created by 张立彬 on 13-7-9.
//  Copyright (c) 2013年 com.rong360. All rights reserved.
//

#import "configViewController.h"
#import "RealEstateQaViewController.h"
#import "DrawView.h"
#import "UMFeedback.h"

@interface configViewController ()

@end

@implementation configViewController


//列表数据
@synthesize DataCopy;

//问答列表
@synthesize DataProduct;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    Dv = [[DrawView alloc]init];
    
    //加载标题栏
    [self titleBar:self.view title:@"关于" btnText:@"完成"];
    
    [self loadScroll];
    
    [self initTableCopy];
    
    [self initTableProduct];
    
    [Dv image:ScrollBody style:@"{{133, 24}, {54, 54}}" src:@"icon"];
    [Dv label:ScrollBody style:@"{{15, 250}, {200, 30}}" text:@"融360出品"];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//加载滚动条
-(void)loadScroll{
    BOOL IsIphone5 =   [UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(320*2, 568*2),[[UIScreen mainScreen] currentMode].size) : NO;
    CGRect _frame = [UIScreen mainScreen].applicationFrame;
    UIScrollView *s = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, 320, _frame.size.height - 40)];
    ScrollBody = s;
    s.pagingEnabled = YES;
    s.showsVerticalScrollIndicator = NO;
    s.showsHorizontalScrollIndicator = NO;
    if(IsIphone5){
        [s setContentSize:CGSizeMake(320,_frame.size.height-30)];
    }else{
        s.showsVerticalScrollIndicator = YES;
        [s setContentSize:CGSizeMake(320,_frame.size.height+35)];
    }

    [self.view addSubview:s];
}

//初始化表格
-(void)initTableCopy{
    //加载表格
    TableCopy = [[UITableView alloc]initWithFrame:CGRectMake(-1, 102, 322, 126) style:UITableViewStylePlain];
    TableCopy.hidden = NO;
    TableCopy.delegate = self;
    TableCopy.dataSource = self;
    TableCopy.scrollEnabled = NO;
    //构造模拟数据
    DataCopy=[NSArray arrayWithObjects:
              [NSArray arrayWithObjects:@"版权",@"copy",@"1.0",nil],
              [NSArray arrayWithObjects:@"意见反馈",@"feed",@"",nil],
              [NSArray arrayWithObjects:@"给我们打分鼓励",@"star",@"",nil]
              ,nil];
    TableCopy.layer.borderWidth = 1;
    TableCopy.layer.borderColor =  [[UIColor colorWithRed:(219/255.0) green:(225/255.0) blue:(228/255.0) alpha:1] CGColor];
    //TableCopy.frame = CGRectMake(-1, 200, 302, 150);
    [ScrollBody addSubview:TableCopy];
}

//初始化表格
-(void)initTableProduct{
    //加载表格
    TableProduct = [[UITableView alloc]initWithFrame:CGRectMake(-1, 285, 322, 198) style:UITableViewStylePlain];
    TableProduct.hidden = NO;
    TableProduct.delegate = self;
    TableProduct.dataSource = self;
    TableProduct.scrollEnabled = NO;
    //构造模拟数据
    //DataProduct=[NSArray arrayWithObjects:@"融360贷款",@"房贷计算器",@"银行理财大全",nil];
    DataProduct=[NSArray arrayWithObjects:
              [NSArray arrayWithObjects:@"融360贷款",@"itms-apps://itunes.apple.com/app/id574727794",@"随时随地比较贷款",@"daikuan",nil],
              [NSArray arrayWithObjects:@"房贷计算器",@"itms-apps://itunes.apple.com/app/id648234582",@"房买房必备",@"jsq",nil],
              [NSArray arrayWithObjects:@"银行理财大全",@"itms-apps://itunes.apple.com/app/id562807895",@"日更新100余额款理财产品",@"licai",nil]
              ,nil];
    TableProduct.layer.borderWidth = 1;
    TableProduct.layer.borderColor =  [[UIColor colorWithRed:(219/255.0) green:(225/255.0) blue:(228/255.0) alpha:1] CGColor];
    //TableCopy.frame = CGRectMake(-1, 200, 302, 150);
    [ScrollBody addSubview:TableProduct];
}


//下面都是实现UITableViewDelegate,UITableViewDataSource两个协议中定义的方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = 0;
    
    if(tableView == TableCopy){
        count = [DataCopy count];
    }
    if(tableView == TableProduct){
        count = [DataProduct count];
    }
    NSLog(@"%d",count);
    return count;
}

//返回一行的视图
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSUInteger row=[indexPath row];
    NSString * tableIdentifier=@"Simple table";

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableIdentifier];
    
    if(cell==nil)
    {
        //当没有可复用的空闲的cell资源时(第一次载入,没翻页)
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];        
    }
    if(tableView == TableCopy){
        //创建表格
        [Dv tr:cell text:DataCopy[row][0] select:@"color:228,236,241" tail:DataCopy[row][2]];
    }
    if(tableView == TableProduct){
        [Dv trRich:cell icon:[UIImage imageNamed:DataProduct[row][3]] text:DataProduct[row][0] select:@"color:228,236,241" detail:DataProduct[row][2]];
        [Dv image:cell.contentView style:@"{{253, 20}, {60, 26}}" src:@"btn"];
    }
    
    return cell;
}

//选中单元格之前触发
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //被选中前执行
    if (tableView == TableCopy) {
     if ([indexPath row]==0) {
         //第一项不可选
         return nil;
     }else{
         if([DataCopy[indexPath.row][1] isEqual: @"feed"]){
             [UMFeedback showFeedback:self withAppkey:kUMFeedbackKey];
         }
         if([DataCopy[indexPath.row][1] isEqual: @"star"]){
             NSString *url = @"https://itunes.apple.com/us/app/fang-dai-zi-ge-ce-shi/id681055306?ls=1&mt=8";
             [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
         }
        
         return nil;
     }
        
    }
    if(tableView == TableProduct){
        NSString *str = DataProduct[indexPath.row][1];
        NSLog(@"%@",str);
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        return nil;
    }
    return indexPath;
}

//选中单元格后触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{//被选中后执行
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    if(cell==nil)
    {
        //当没有可复用的空闲的cell资源时(第一次载入,没翻页)
        cell=[[UITableViewCell alloc] init];
    }

    NSString *string = @"demo";
    if(tableView == TableCopy){
        string=DataCopy[indexPath.row][1];
        //设置点击后背景
        UIView *bgColorView = [[UIView alloc] init];
        bgColorView.backgroundColor = [UIColor colorWithRed:(255/255.0) green:(255/255.0) blue:(255/255.0) alpha:1];
        [cell setSelectedBackgroundView:bgColorView];
    }
    if(tableView == TableProduct){
        string=DataProduct[indexPath.row][1];
    }
    //取出被选中项的文字
    //UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:string delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    //[alert show];
    
    
    if(tableView == TableCopy){
        NSLog(@"DataList");
    }
    if(tableView == TableProduct){
        NSLog(@"QaList");
    }
}

//更改高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //返回每行的高度
    NSInteger height = 42;
    if(tableView == TableProduct){
        height = 66;
    }
    return height;
}

//返回首页
-(IBAction)TapReturn:(id)sender{
    //自定义动画
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.3];
    [animation setType: kCATransitionMoveIn];
    [animation setSubtype: kCATransitionFromBottom];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    [self.navigationController.view.layer addAnimation:animation forKey:nil];
    [self.navigationController popViewControllerAnimated:NO];
}

//画标题栏
-(void)titleBar:(UIView *)wrap title:(NSString *)title btnText:(NSString *)btnText
{
    
    UIView *foo = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    foo.backgroundColor = [UIColor colorWithRed:(26/255.0) green:(173/255.0) blue:(242/255.0) alpha:1];
    [wrap addSubview:foo];
    
    //加载标题
    //创建uilabel
    UILabel *tit = [[UILabel alloc] initWithFrame:CGRectMake(110, 6, 100, 30)];
    
    //设置背景色
    tit.backgroundColor = [UIColor clearColor];
    
    //设置tag
    //label1.tag = 91;
    
    //设置标签文本
    tit.text = title;
    //设置标签文本字体和字体大小
    tit.font = [UIFont fontWithName:@"Helvetica" size:22];
    tit.textAlignment = 1;
    //文本颜色
    tit.textColor = [UIColor whiteColor];
    
    [foo addSubview:tit];
    
    //加载完成按钮
    //UIButton *returnHome = btn;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(245, 6, 73, 33);
    [btn setTitle:btnText forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    [btn addTarget:self action:@selector(TapReturn:) forControlEvents:UIControlEventTouchUpInside];
    
    [foo addSubview:btn];
    
}


@end
